﻿using ArcGIS.Core.CIM;
using ArcGIS.Core.Data;
using ArcGIS.Core.Geometry;
using ArcGIS.Desktop.Catalog;
using ArcGIS.Desktop.Core;
using ArcGIS.Desktop.Editing;
using ArcGIS.Desktop.Extensions;
using ArcGIS.Desktop.Framework;
using ArcGIS.Desktop.Framework.Contracts;
using ArcGIS.Desktop.Framework.Dialogs;
using ArcGIS.Desktop.Framework.Threading.Tasks;
using ArcGIS.Desktop.Layouts;
using ArcGIS.Desktop.Mapping;
using CCTool.Scripts.Manager;
using CCTool.Scripts.ToolManagers.Extensions;
using CCTool.Scripts.ToolManagers.Managers;
using CCTool.Scripts.ToolManagers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;

namespace CCTool.Scripts.GDBMenu
{
    internal class RepireFeatureClass : Button
    {
        // 定义一个进度框
        private ProcessWindow processwindow = null;
        string tool_name = "整库修复几何";

        protected override async void OnClick()
        {
            try
            {

                // 打开进度框
                ProcessWindow pw = UITool.OpenProcessWindow(processwindow, tool_name);
                pw.AddMessageTitle(tool_name);


                await QueuedTask.Run(() =>
                {

                    // 获取当前选择的gdb数据库
                    string gdbPath = Project.Current.SelectedItems.OfType<GDBProjectItem>().FirstOrDefault().Path;
                    // 要素类
                    List<string> originFCList = gdbPath.GetFeatureClassPathFromGDB();

                    pw.AddMessageStart("修复要素类");
                    int index = 1;
                    foreach (string fc in originFCList)
                    {
                        string fcName = fc[(fc.LastIndexOf(".gdb") + 5)..];
                        pw.AddMessageMiddle(10, $"【{index}】{fcName}", Brushes.Gray);

                        Arcpy.RepairGeometry(fc);

                        index ++;
                    }

                });
                pw.AddMessageEnd();
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.Message + ee.StackTrace);
                return;
            }
        }
    }
}
